home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1998 July / EnigmA AMIGA RUN 29 (1998)(G.R. Edizioni)(IT)[!][issue 1998-07 & 08].iso / earcd / grafica / ghostscript / 5.10 / data / writecff.ps < prev    next >
Text File  |  1997-09-19  |  11KB  |  367 lines

  1. %    Copyright (C) 1997 Aladdin Enterprises.  All rights reserved.
  2. % This file is part of Aladdin Ghostscript.
  3. % Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  4. % or distributor accepts any responsibility for the consequences of using it,
  5. % or for whether it serves any particular purpose or works at all, unless he
  6. % or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  7. % License (the "License") for full details.
  8. % Every copy of Aladdin Ghostscript must include a copy of the License,
  9. % normally in a plain ASCII text file named PUBLIC.  The License grants you
  10. % the right to copy, modify and redistribute Aladdin Ghostscript, but only
  11. % under certain conditions described in the License.  Among other things, the
  12. % License requires that the copyright notice and this notice be preserved on
  13. % all copies.
  14.  
  15. % writecff.ps
  16. % Write out a Type 2 font as CFF.
  17. % **************** THIS FILE DOES NOT WORK. ****************
  18. % **************** DON'T TRY TO USE IT. ****************
  19.  
  20. currentglobal true setglobal
  21. (gs_cff.ps) runlibfile
  22. setglobal
  23.  
  24. 50 dict begin
  25.  
  26. % ---------------- Standard strings/names ---------------- %
  27.  
  28. /FontSetInit /ProcSet findresource begin mark
  29.  
  30. /StandardSIDs StandardStrings length 1.5 mul cvi dict
  31. dup 0 1 StandardStrings length 1 sub {
  32.         % Stack: mark /StdSIDs dict dict index
  33.   dup StandardStrings exch get exch put dup
  34. } for pop
  35.  
  36. end counttomark 2 idiv { def } repeat pop
  37.  
  38. % ---------------- Standard encodings ---------------- %
  39.  
  40. % ---------------- Standard Charsets ---------------- %
  41.  
  42. % ---------------- Output utilities ---------------- %
  43.  
  44. % Free variables: f (output file), fpos (position in f).
  45.  
  46. /advance {        % <n> advance -
  47.   fpos add /fpos exch store
  48. } def
  49. /next {            % <byte> next -
  50.   f exch write 1 advance
  51. } def
  52. /nextstring {        % <string|name> nextstring -
  53.   dup type /nametype eq { .namestring } if
  54.   f 1 index writestring length advance
  55. } def
  56. /card8            % <card8> card8 -
  57.   /next load
  58. def
  59. /card16 {        % <card16> card16 -
  60.   dup -8 bitshift next 255 and next
  61. } def
  62. /offset {        % <offset> <offsize> offset -
  63.   1 sub -1 0 { -8 mul 2 copy bitshift 255 and next pop } for pop
  64. } def
  65. /sid            % <sid> sid -
  66.   /card16 load
  67. def
  68. /lenoffsize {        % <length> lenoffsize <offsize>
  69.   dup 255 le { pop 1 } { -8 bitshift lenoffsize 1 add } ifelse
  70. } def
  71. /Index {        % [<string|name|null> ...] Index -
  72.     % Calculate the maximum offset we need to be able to represent.
  73.   1 1 index { dup null eq { pop } { length add } ifelse } forall lenoffsize
  74.     % stack: items offsize
  75.   1 index length card16
  76.   dup next
  77.   1 2 index {
  78.       % stack: items offsize pos item
  79.     1 index 3 index offset
  80.     dup null eq { pop } { length add } ifelse
  81.   } forall exch offset { nextstring } forall
  82. } def
  83. /idIndex {        % <dict> <base> idIndex -
  84.   1 index length array 3 -1 roll {
  85.     3 index sub 2 index exch 3 -1 roll put
  86.   } forall exch pop Index
  87. } def
  88. /stringid {        % <string|name> stringid <sid>
  89.   StandardSIDs 1 index .knownget {
  90.     exch pop
  91.   } {
  92.     sids 1 index .knownget {
  93.       exch pop
  94.     } {
  95.       StandardSIDs length sids length add sids 3 -1 roll 2 index put
  96.     } ifelse
  97.   } ifelse
  98. } def
  99. /.valuetypedict mark
  100.   /booleantype { { 1 } { 0 } ifelse intvalue }
  101.   /arraytype { { value } forall }
  102.   /packedarraytype 1 index
  103.   /stringtype { stringid intvalue }
  104.   /nametype 1 index
  105.   /realtype { realvalue }
  106.   /integertype { intvalue }
  107. .dicttomark readonly def
  108. /value {        % <obj> value -
  109.   dup type .valuetypedict exch get exec
  110. } def
  111. /.realchardict mark
  112.   48 1 57 { dup 48 sub } for    % digits
  113.   46 10   69 11   101 11    % . E e
  114.   45 {                % - -- handle E- specially
  115.     dup 15 and 11 eq { 15 or 12 } {
  116.       dup 16#bf eq { pop 255 12 } { 14 } ifelse
  117.     } ifelse
  118.   }
  119. .dicttomark readonly def
  120. /realvalue {        % <real> realvalue -
  121.   =string cvs 255 exch {
  122.     .realchardict exch get exec
  123.     1 index 15 and 15 ne { exch next 255 exch } if
  124.     1 index 240 and 240 eq { 4 bitshift 240 } { 15 } ifelse sub add
  125.   } forall next
  126. } def
  127. /intvalue {        % <int> intvalue -
  128.   dup dup -107 ge exch 107 le and { 139 add next } {
  129.     dup dup -1131 ge exch 0 lt and { neg 16#fa94 add card16 } {
  130.       dup dup 1131 le exch 0 ge and { 16#f694 add card16 } {
  131.     dup dup -32768 ge exch 32767 le and {
  132.       28 next 65535 and card16
  133.     } {
  134.       29 next dup -16 bitshift 2 { 65535 and card16 } repeat
  135.     } ifelse
  136.       } ifelse
  137.     } ifelse
  138.   } ifelse
  139. } def
  140. /op {            % <op> op -
  141.   dup 32 ge { 12 next 32 sub } if next
  142. } def
  143. /vdef {            % <obj> <op> vdef -
  144.   exch value op
  145. } def
  146. /nedef {        % <obj> <default> <op> nedef -
  147.   exch 2 index eq { pop pop } { vdef } ifelse
  148. } def
  149. /Dict {            % <dict> <opsdict> Dict -
  150.   exch {
  151.     % stack: opsdict key value
  152.     2 index 3 -1 roll .knownget {
  153.       dup type /integertype eq { vdef } { exec } ifelse
  154.     } {
  155.       pop
  156.     } ifelse
  157.   } forall pop
  158. } def
  159. /collect {        % <proc> collect <string>
  160.   10 dict begin
  161.   /str 500 string def
  162.   /spos 0 def
  163.   /fpos 0 def
  164.   /f {
  165.     pop length spos add /spos exch store
  166.     spos str length eq { /str str str concatstrings def } if
  167.     str spos str length spos sub getinterval
  168.   } /NullEncode filter def
  169.   exec f closefile
  170.   str 0 spos getinterval end
  171. } def
  172.  
  173. % Write a forward reference in a Dict, and save its position.
  174. /forward {        % <varname> <op> <offsize> forward -
  175.   {null null <1c 00 00> <1d 00 00 00 00>} exch get nextstring
  176.   exch fpos store op
  177. } def
  178.  
  179. % ------ Top (font) dictionary ------ %
  180.  
  181. /topkeyops mark
  182.   /version 0
  183.   /Notice 1
  184.   /Copyright 32
  185.   /FullName 2
  186.   /FamilyName 3
  187.   /Weight 4
  188.   /isFixedPatch { false 33 nedef }
  189.   /ItalicAngle { 0 34 nedef }
  190.   /UnderlinePosition { -100 35 nedef }
  191.   /UnderlineThickness { 50 36 nedef }
  192.   /PaintType { 0 37 nedef }
  193.   /CharstringType { 2 38 nedef }
  194.   /FontMatrix {
  195.     true 0 1 5 {
  196.       2 index 1 index get {0.001 0 0 0.001 0 0} 3 -1 roll get eq and
  197.     } for
  198.     { pop } { 39 vdef } ifelse
  199.   }
  200.   /UniqueID 13
  201.   /FontBBox 5
  202.   /StrokeWidth { 0 40 nedef }
  203.   /XUID 14
  204.   /FontInfo { topkeyops Dict }
  205.     %**** charset, Encoding
  206.   /CharStrings { pop /charstringsoffset 17 offsetsize forward }
  207.   /Private { pop /privateoffset 18 offsetsize forward }
  208. .dicttomark readonly def
  209.  
  210. % ------ Private dictionary ------ %
  211.  
  212. /deltarray {        % [<num> ...] <op> deltarray -
  213.   exch 0 exch { 1 index sub dup value add } forall pop op
  214. } def
  215. /privatekeyops mark
  216.   /BlueValues { 6 deltarray }
  217.   /OtherBlues { 7 deltarray }
  218.   /FamilyBlues { 8 deltarray }
  219.   /FamilyOtherBlues { 9 deltarray }
  220.   /BlueScale { 0.039625 41 nedef }
  221.   /BlueShift { 7 42 nedef }
  222.   /BlueFuzz { 1 43 nedef }
  223.   /StdHW 10
  224.   /StdVW 11
  225.   /StemSnapH { 44 deltarray }
  226.   /StemSnapV { 45 deltarray }
  227.   /ForceBold { false 46 nedef }
  228.   /ForceBoldThreshold { 0 47 nedef }
  229.     % Skip lenIV, it's always -1
  230.   /LanguageGroup { 0 49 nedef }
  231.   /ExpansionFactor { 0.06 50 nedef }
  232.   /initialRandomSeed { 0 51 nedef }
  233.   /Subrs { pop /subrsoffset 19 2 forward }
  234.   /defaultWidthX { 0 20 nedef }
  235.   /nominalWidthX { 0 21 nedef }
  236. .dicttomark readonly def
  237.  
  238. % ------ Main program ------ %
  239.  
  240. /putoffset {        % <str> <index> <offset> <offsize> putoffset -
  241.     % The saved index points just beyond the end of the number,
  242.     % and we wrote a zero as the placeholder.
  243.     % Consequently, we don't need the offset size.
  244.   pop {
  245.     dup 0 eq { exit } if
  246.     exch 1 sub exch 3 copy 255 and put -8 bitshift
  247.   } loop pop pop pop
  248. } def
  249.  
  250. /writecff {        % <file> [<font> ...] writecff -
  251.   30 dict begin
  252.     % The dictionary for each font contains:
  253.     %    font - the original Type 2 font
  254.     %    subrs - the Local Subrs Index
  255.     %    chars - the CharStrings Index
  256.     %    private - the Private Dict
  257.     %    top - the Top Dict
  258.     %    charspos - the offset of the CharStrings Dict relative to
  259.     %      the start of all CharStrings
  260.     %    privatepos - the offset of the Private Dict relative to
  261.     %      the start of all Private Dicts
  262.     %    subrsoffset - the offset of the Local Subrs offset
  263.     %      reference in the Private Dict
  264.     %    charstringsoffset - the offset of the CharStrings offset
  265.     %      reference in the Top dict
  266.     %    privateoffset - the offset of the Private Dict offset
  267.     %      reference in the Top dict
  268.   [ exch { 12 dict begin /font exch cvlit def currentdict end } forall ]
  269.   /fonts exch def
  270.   /cff exch cvlit def
  271.  
  272.   /f cff def
  273.   /fpos 0 def
  274.  
  275.     % We need to pre-construct all the strings so that we know
  276.     % the offsets to fill in.
  277.  
  278.   /names { [ fonts { /font get /FontName get } forall ] Index } collect def
  279.   /sids 20 dict def
  280.   /subrslength 0 def        % only for estimating size
  281.   /charslength 0 def
  282.   /privatelength 0 def
  283.   fonts {
  284.     begin
  285.     font /Private get /Subrs .knownget {
  286.       { Index } collect
  287.     } {
  288.       <00 00>
  289.     } ifelse /subrs exch def
  290.     /subrslength subrslength subrs length add def
  291.     %****** FOLLOWING IS WRONG, WRONG, WRONG ******%
  292.     font /CharStrings get [ exch { exch pop } forall ] { Index } collect
  293.     /chars exch def
  294.     /charspos charslength def
  295.     /charslength charslength chars length add def
  296.     /subrsoffset null def
  297.     font /Private get { privatekeyops Dict } collect /private exch def
  298.     subrsoffset null ne {
  299.       private subrsoffset private length 2 putoffset
  300.     } if
  301.     /privatepos privatelength def
  302.     /privatelength privatelength private length add subrs length add def
  303.   } forall
  304.     % Estimate the size of a 0-based offset for the Top Dicts.
  305.   /offsetsize subrslength charslength add privatelength add
  306.     60000 ge { 3 } { 2 } ifelse def
  307.   fonts {
  308.     begin
  309.     /charstringsoffset null def
  310.     /privateoffset null def
  311.     /top { font topkeyops Dict } collect def
  312.     end
  313.   } forall
  314.   /strings { sids StandardSIDs length idIndex } collect def
  315.  
  316.     % Now we can write the real file.
  317.     % Header
  318.   DEBUG { (header ) print fpos == } if
  319.   <01 00 04 02> nextstring
  320.     % Name Index
  321.   DEBUG { (names ) print fpos == } if
  322.   names nextstring
  323.     % Top Dicts
  324.   DEBUG { (tops ) print fpos == } if
  325.   /charsbase fpos fonts { /top get length add } forall strings length add def
  326.   /privatebase charsbase charslength add def
  327.   fonts {
  328.     begin
  329.     top charstringsoffset charspos charsbase add offsetsize putoffset
  330.     top privateoffset privatepos privatebase add offsetsize putoffset
  331.     top nextstring
  332.     end
  333.   } forall
  334.     % String Index
  335.   DEBUG { (strings ) print fpos == } if
  336.   strings nextstring
  337.     % Charstrings Indexes
  338.   DEBUG { (charstrings) print } if
  339.   /charsbase fpos def
  340.   fonts {
  341.     DEBUG { ( ) print fpos =only } if /chars get nextstring
  342.   } forall
  343.   DEBUG { () = } if
  344.     % Private Dicts & Local Subr Indexes
  345.   DEBUG { (privates/subrs) print } if
  346.   fonts {
  347.     DEBUG { ( ) print fpos =only } if dup /private get nextstring
  348.     DEBUG { ( ) print fpos =only } if /subrs get nextstring
  349.   } forall
  350.   DEBUG { () = } if
  351.  
  352.   DEBUG { (end ) print fpos = flush } if
  353.   end
  354. } def
  355.  
  356. % ---------------- Wrap up ---------------- %
  357.  
  358. currentdict readonly end
  359. /writecffdict exch def
  360.  
  361. /writecff {
  362.   writecffdict begin writecff end
  363. } def
  364.